package fragments;

import info.androidhive.slidingmenu.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import mindshare.JSONParser;
import mindshare.LoadContent;
import android.app.Activity;
import android.content.Context;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class WhatsHotFragment extends Fragment {
	JSONParser jParser = new JSONParser();
	ArrayAdapter <String> adpCities;
	public List<String> countriesList= new ArrayList<String>();
	public List<String> citiesList= new ArrayList<String>();
	private int lastPosition=0;
	List<NameValuePair> params = new ArrayList<NameValuePair>();
	
		
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		final View rootView = inflater.inflate(R.layout.fragment_whats_hot, container, false);
		Spinner spcountries = (Spinner) rootView.findViewById(R.id.spCountries);
        spcountries.setOnItemSelectedListener(new onItemSelect());
		Context c= rootView.getContext();
		params.clear();
		loadCountries loadCountries= new loadCountries( c, params,"countries");
		loadCountries.execute();
        return rootView;
    }
	
	class onItemSelect implements OnItemSelectedListener{
		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
			if (lastPosition != position) {
	    		Context c= view.getContext();
	    		String countryParam=parent.getItemAtPosition(position).toString();
	    		params.clear();
	     		params.add(new BasicNameValuePair("country", countryParam));
	    		loadCities load= new loadCities(c, params, "cities");
	        	load.execute();
	    	}
	    	lastPosition = position;
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {}
	}

	class loadCities extends LoadContent{
		public loadCities(Context context, List<NameValuePair> param, String data) {
			super(context, param, data);
		}
		
		@Override
		protected void onPostExecute(String file_url) {
			adpContent= new ArrayAdapter <String>( (Activity)c, android.R.layout.simple_list_item_1, android.R.id.text1, contentList);
	    	adpContent.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    	Context c= getActivity();
			Spinner spcities = (Spinner) ((Activity)c).findViewById(R.id.spCities);
	    	spcities.setAdapter(adpContent);
	        pDialog.dismiss();
		}
		
	}
	
	class loadCountries extends LoadContent{
		public loadCountries(Context context, List<NameValuePair> param, String data) {
			super(context, param, data);
		}
		
		@Override
		protected void onPostExecute(String file_url) {
			adpContent= new ArrayAdapter <String>( (Activity)c, android.R.layout.simple_list_item_1, android.R.id.text1, contentList);
	    	adpContent.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    	Context c= getActivity();
			Spinner spcountries = (Spinner) getView().findViewById(R.id.spCountries);
	    	spcountries.setAdapter(adpContent);
	        List<NameValuePair> country = new ArrayList<NameValuePair>();
	        country.add(new BasicNameValuePair("country", "Argentina"));
	        LoadContent load= new loadCities(c, country, "cities");
	    	load.execute();
	        pDialog.dismiss();
		}
	}
	
}
